package com.itheima.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.category.mapper.CategoryDao;
import com.itheima.category.service.CategoryService;
import com.itheima.goods.pojo.Category;
import entity.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wang
 * @description
 * @date 2021/7/30 9:14
 */
@Service("categoryService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Override
    public List<Category> findAll() {
        return this.list();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public boolean add(Category category) {
        return this.save(category);
    }

    @Override
    public PageResult findPage4Get(Integer page, Integer size) {
        //设置分页参数
        IPage pageInfo = new Page(page,size);
        //查询条件
        LambdaQueryWrapper<Category> wrapper = Wrappers.lambdaQuery(Category.class);
        wrapper.orderByAsc(Category::getSeq).orderByAsc(Category::getId);
        this.page(pageInfo,wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setRows(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public PageResult findPage4Post(Integer page, Integer size,Category category) {
        //设置分页参数
        IPage pageInfo = new Page(page,size);
        //查询条件
        LambdaQueryWrapper<Category> wrapper = Wrappers.lambdaQuery(Category.class);
        if (category.getName() != null && !"".equals(category.getName().trim())) {
            wrapper.like(Category::getName,category.getName());
        }
        if (category.getIsShow() != null && !"".equals(category.getIsShow().trim())) {
            wrapper.eq(Category::getIsShow,category.getIsShow());
        }
        if (category.getIsMenu() != null && !"".equals(category.getIsMenu().trim())) {
            wrapper.eq(Category::getIsMenu,category.getIsMenu());
        }
        if (category.getSeq() != null) {
            wrapper.eq(Category::getSeq,category.getSeq());
        }
        if (category.getParentId() != null) {
            wrapper.eq(Category::getParentId,category.getParentId());
        }
        wrapper.orderByAsc(Category::getSeq).orderByAsc(Category::getId);
        this.page(pageInfo,wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setRows(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public Category findById(Integer id) {
        return this.getById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public boolean deleteById(Integer id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public boolean updateCategory(Category category, Integer id) {
        category.setId(id);
        return this.updateById(category);
    }
}
